import { WeApp } from '../../../../common/common';
import { config } from '../../../../etc/config';
import httpClient from '../../../../common/http-client';
import { Consignee } from '../../../../model/consignee';
import wxService from '../../../../common/wxService'
import tools from '../../../../common/tools'
import { prompt } from '../../../../assets/components/wux';
import utils from '../../../../utils/utils';

const app: WeApp = getApp() as WeApp;
interface UserAddressListPageData {
    consignee?: Array<Consignee>,
    isSelect?: boolean,
    noId?:string,
}

interface IAddressModel {
    id?: string,
    name?: string,
    address?: string,
    phone?: string,
    isDefault?: boolean,
    areas?: Array<string>,
}

interface UserAddressListPage extends IPage { }

class UserAddressListPage {
    public data: UserAddressListPageData = {
    }

    public onLoad(option): void {
        this.setData({
            'isSelect': option.isSelect,
            'currentId': option.cid
        });
    }
    
    public onShow(): void {
        setTimeout(() => {
            this.getList();
        }, 1);
    }

    public navigateTo(e): void {
        const id = e.target.dataset.id;
        wxService.navigateTo(`../../goods/list/index?nid=${id}`);
    }

    public toAddressEdit(e) {
        const target = e.currentTarget.dataset;
        if (target.id) {
            wx.setStorage({
                key: "addressedit",
                data: this.data.consignee[e.currentTarget.dataset.index]
            });
        }
        wxService.navigateTo(tools.buildUrl(`/pages/user/address/edit/index`, { id: target.id, isSelect: this.data.isSelect }));
    }


    public setDefalutAddress(e) {
        const id = e.currentTarget.dataset.id
        const param = this.data.consignee[e.currentTarget.dataset.index];
        if (param.isDefault) {
            return;
        }
        param.isDefault = true;
        httpClient.post(config.consignee.consignee_save, param)
            .then(res => {
                if (res.succeed) {
                    wxService.showToast('设置成功');                    
                    app.setCurUserDefaultConsignee(param);
                    this.getList();
                } else {
                    wxService.showToast(res.msg);
                }
            });
    }

    public getList() {
        httpClient.get<Consignee>(config.consignee.consignee_all)
            .then((res) => {
                if (res.succeed) {
                    this.setData({
                        consignee: res.values
                    })
                    const prop = prompt.init('msg1', {
                        title: '',
                        text: '还没有收货地址哦，赶快添加一个！',
                        icon:'http://r.59im.com/wx/tips/lack_without_address_icon@2x.png'
                    });
                    if (!res.values || !res.values.length) {
                        prop.show();
                    }
                    else {
                        prop.hide();
                    }
                }
                else {
                    wxService.showToast(res.msg);
                }
            }).catch(e=>{
                utils.loadError(()=>{
                    this.getList();
                })
            })
    }

    toAddressDelete(e) {
        const target = e.currentTarget.dataset;
        wxService.showModal({ content: '是否确认删除此地址？', confirmColor: '#1972ff' })
            .then((res: wx.ShowModalResult) => {
                if (res.confirm) {
                    httpClient.post<Consignee>(config.consignee.consignee_remove, { id: target.id })
                        .then(res => {
                            if (res.succeed) {
                                this.getList();
                            } else {
                                wxService.showToast(res.msg);
                            }
                        })
                }
            })
    }

    selectConsignee(e) {
        if (!this.data.isSelect) {
            this.toAddressEdit(e);
            return;
        }
        const consignee = this.data.consignee[e.currentTarget.dataset.index];
        wx.setStorageSync(config.storageNames.confirmOrderConsignee, consignee)
        wx.navigateBack();
    }
}

Page(new UserAddressListPage());